<?php
require_once("includes/pagetop.php");
if(!isset($_SESSION['Allyfolio_User_ID']) || $_SESSION['Allyfolio_User_ID']==0)
{
	echo "ERROR|~|~|Please refresh the page and try again.";
}
else 
{
	
	$startFrom=0;
	$totalRecordsPerPage=5;
	$loginUserID=$_SESSION['Allyfolio_User_ID'];
	$message_status=$_REQUEST["message_status"];
	
	if($_REQUEST["action"]=="getNextMessagedFriend" && $_REQUEST["currentFriendsCountInLeft"]>0)
	{
		$leftMesasgedFriend=$_REQUEST["currentFriendsCountInLeft"];
		
	    $qryFetchAllMessagedFriends="select af_users.*,tempchat.* from af_users, (select *,if(`from`='".$loginUserID."', `to`, `from`) as friendID from cometchat where (`from`='".$loginUserID."' or `to`='".$loginUserID."') and message_status='".$message_status."' order by sent desc) as tempchat where af_users.User_ID<>'".$loginUserID."'  and ((af_users.User_ID=tempchat.`from` and tempchat.`to`='".$loginUserID."' ) or (af_users.User_ID=tempchat.`to` and tempchat.`from`='".$loginUserID."')) group by af_users.User_ID limit ".$leftMesasgedFriend.", ".$totalRecordsPerPage;
	    $arrFetchAllMessagedFriends = $pdodbobj->fetch_array($pdodbobj->query($qryFetchAllMessagedFriends,array()));
		
		echo "SUCCESS|~|~|";
		
		if(count($arrFetchAllMessagedFriends)>0)
		{
			for($i=0; $i<count($arrFetchAllMessagedFriends) && $i<$totalRecordsPerPage; $i++)
			{
				//check All Read or not
				$qryCheckAllRead="select count(*) as unreadCount from cometchat where `to`='".$loginUserID."' and `from`='".$arrFetchAllMessagedFriends[$i]["friendID"]."' and `read`=0  and message_status='".$message_status."' ";
				$arrCheckAllRead = $pdodbobj->fetch_array($pdodbobj->query($qryCheckAllRead,array()));
						
				$leftMesasgedFriend++;
				$sentDate=getTimeAgo($arrFetchAllMessagedFriends[$i]["sent"], time());
		?>
			<tr <?php if($arrCheckAllRead[0]["unreadCount"]>0){ ?>style="background: #EEE;"<?php } ?> id="left_messaged_friend_tr_<?php echo $arrFetchAllMessagedFriends[$i]["User_ID"]; ?>" onclick="displayFullMessage('<?php echo $arrFetchAllMessagedFriends[$i]["User_ID"]; ?>');" >
			<?php
				if(($i+$leftMesasgedFriend)%2!=0)
				{
			?>
				<td width="62" style="padding:0px;">
					<div class="noti_pic"><a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrFetchAllMessagedFriends[$i]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a></div>
				</td>
				<td width="319" valign="top" style="text-align:left;">
					<div style="position:relative; top:0px"><a href="javascript:void(0);"><span class="noti_name" style="margin-top:5px;"><?php echo $arrFetchAllMessagedFriends[$i]["Full_Name"] ?></span></a></div>
					<div class="separator"></div>
					<span style="margin-top:10px; float:left; margin-right:5px;">
					<?php 
						displayFormatedMessage($arrFetchAllMessagedFriends[$i]["message"],$arrFetchAllMessagedFriends[$i]["message_type"]);
					?>
					</span>
				</td>
				<td width="77" align="right" valign="top">
					<span style="margin-top:5px; float:right; margin-right:5px;"><?php echo $sentDate; ?></span>
				</td>
			<?php		
				}
				else
				{
			?>
				<td width="77" align="right" valign="top">
					<span style="margin-top:5px; float:left; margin-right:5px;"><?php echo $sentDate; ?></span>
				</td>
				<td width="319" valign="top" style="text-align:right;">
					<div style="position:relative; top:0px"><a href="javascript:void(0);"><span class="noti_name" style="margin-top:5px;"><?php echo $arrFetchAllMessagedFriends[$i]["Full_Name"] ?></span></a></div>
					<div class="separator"></div>
					<span style="margin-top:10px; float:right; margin-right:5px;">
					<?php 
						displayFormatedMessage($arrFetchAllMessagedFriends[$i]["message"],$arrFetchAllMessagedFriends[$i]["message_type"]);
					?>
					</span>
				</td>
				<td width="62" style="padding:0px;">
					<div class="noti_pic"><a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrFetchAllMessagedFriends[$i]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a></div>
				</td>
			<?php		
				}
			?>	
			</tr>
		<?php		
			}
		}
		else
		{
		?>
			<tr>
				<td colspan="3">There is no messages.</td>
			</tr>
		<?php
		}
		echo "|~|~|".$leftMesasgedFriend;	
	}
	else if(($_REQUEST["action"]=="getFullMessage" || $_REQUEST["action"]=="getPreviousMessages" ) && $_REQUEST["friendId"]>0)
	{
		$qryLoginUserInfo="select * from af_users where User_ID='".$loginUserID."'";
		$arrLoginUserInfo = $pdodbobj->fetch_array($pdodbobj->query($qryLoginUserInfo,array()));
		
		$recentFriend=$_REQUEST["friendId"];
		$qryFetchFriendInfo="select * from af_users where User_ID='".$recentFriend."'";
		$arrFetchAllMessagedFriends = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFriendInfo,array()));
		
		$condition="";
		if($_REQUEST["action"]=="getPreviousMessages"  && $_REQUEST["lastMessageID"]>0)
		{
			$condition=" and id < '".$_REQUEST["lastMessageID"]."' ";
		}
		
		$qryFetchFullMessages="select * from cometchat where ((`from`='".$loginUserID."' and `to`='".$recentFriend."') or (`to`='".$loginUserID."' and `from`='".$recentFriend."'))  and message_status='".$message_status."' ".$condition." order by sent desc limit ".$startFrom.", ".$totalRecordsPerPage;
		$arrFetchFullMessages = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFullMessages,array()));
		
		if($_REQUEST["action"]=="getFullMessage")
		{
			//make all unread as read
			$qryMakeRead="update cometchat set `read`=1 where `to`='".$loginUserID."' and `from`='".$recentFriend."' and message_status='".$message_status."'  ";
			$pdodbobj->query($qryMakeRead,array());
		}
		
		$qryFetchFullMessagesCount="select count(*) as totalRows from cometchat where ((`from`='".$loginUserID."' and `to`='".$recentFriend."') or (`to`='".$loginUserID."' and `from`='".$recentFriend."'))  and message_status='".$message_status."' ".$condition." order by sent limit ".$startFrom.", ".$totalRecordsPerPage;
		$arrFetchFullMessagesCount = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFullMessagesCount,array()));
		
		if(count($arrFetchFullMessages)>0)
		{
			echo "SUCCESS|~|~|";
			
			$lastMessageID="";
			for($j=(count($arrFetchFullMessages)-1); $j>=0; $j--)
			{
				if($lastMessageID=="")
				{
					$lastMessageID=$arrFetchFullMessages[$j]["id"];
				}
				$sentMsgDate=getTimeAgo($arrFetchFullMessages[$j]["sent"], time());
		?>
				<tr  class="right_side_message_tr">
			<?php
				if($arrFetchFullMessages[$j]["from"]!=$loginUserID)
				{
			?>
					<td class="delete_box_td" >
						<input type="checkbox" id="delete_box_checkbox_<?php echo $arrFetchFullMessages[$j]["id"]; ?>" name="delete_box_checkbox[]" class="delete_box_checkbox" value="<?php echo $arrFetchFullMessages[$j]["id"]; ?>" />
					</td>
					<td width="62" style="padding:0px;">
						<div class="noti_pic">
							<a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrFetchAllMessagedFriends[0]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a>
						</div>
					</td>
					<td width="319" valign="top" style="text-align:left;">
						<div style="position:relative; top:0px"><span class="noti_name" style="margin-top:5px;"><a href="javascript:void(0)"><?php echo $arrFetchAllMessagedFriends[0]["Full_Name"] ?></a></span></div>
						<div class="separator"></div>
						<span style="margin-top:10px; float:left; margin-right:5px;">
						<?php 
							displayFormatedMessage($arrFetchFullMessages[$j]["message"],$arrFetchFullMessages[$j]["message_type"]);
						?>
						</span>
					</td>
					<td width="77" align="right" valign="top">
						<span style="margin-top:5px; float:right; margin-right:5px;"><?php echo $sentMsgDate; ?></span>
					</td>
			<?php		
				}
				else
				{
			?>
					<td width="77" align="right" valign="top">
						<span style="margin-top:5px; float:left; margin-right:5px;"><?php echo $sentMsgDate; ?></span>
					</td>
					<td width="319" valign="top" style="text-align:right;">
						<div style="position:relative; top:0px"><span class="noti_name" style="margin-top:5px;">
						<?php echo $arrLoginUserInfo[0]["Full_Name"] ?>
						</span></div>
						<div class="separator"></div>
						<span style="margin-top:10px; float:right; margin-right:5px;">
						<?php 
							displayFormatedMessage($arrFetchFullMessages[$j]["message"],$arrFetchFullMessages[$j]["message_type"]);
						?>
						</span>
					</td>
					<td width="62" style="padding:0px;">
						<div class="noti_pic"><a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrLoginUserInfo[0]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a></div>
					</td>
					<td class="delete_box_td" >
						<input type="checkbox" id="delete_box_checkbox_<?php echo $arrFetchFullMessages[$j]["id"]; ?>" name="delete_box_checkbox[]" class="delete_box_checkbox" value="<?php echo $arrFetchFullMessages[$j]["id"]; ?>" />
					</td>
				<?php		
					}
				?>	
				</tr>
		<?php		
			}
			echo "|~|~|".$lastMessageID;
			
			if($arrFetchFullMessagesCount[0]["totalRows"]>count($arrFetchFullMessages))
			{
				echo "|~|~|"."YES"; //To display more button
			}
			else
			{
				echo "|~|~|"."NO";
			}
			//Get Unread Message Count
			$qryFetchUnreadMessagesCount="select count(*) as unreadcount from cometchat where `to`='".$loginUserID."' and `read`=0  and message_status='".$message_status."' ";
			$arrFetchUnreadMessagesCount = $pdodbobj->fetch_array($pdodbobj->query($qryFetchUnreadMessagesCount,array()));
			echo "|~|~|".$arrFetchUnreadMessagesCount[0]["unreadcount"];	
				
		}
		else
		{
			echo "ERROR|~|~|There is no messages.";
		}
	}
	else if((($_REQUEST["action"]=="sendMessages" && trim($_REQUEST["message"])!="") || $_REQUEST["action"]=="refreshMessages" || $_REQUEST["action"]=="uploadMediaMessage") && $_REQUEST["otheruserid"]>0 && $_REQUEST["firstmessageid"]>0)
	{
		include_once("smiley.php");
		
		$qryLoginUserInfo="select * from af_users where User_ID='".$loginUserID."'";
		$arrLoginUserInfo = $pdodbobj->fetch_array($pdodbobj->query($qryLoginUserInfo,array()));
		
		$recentFriend=$_REQUEST["otheruserid"];
		$qryFetchFriendInfo="select * from af_users where User_ID='".$recentFriend."'";
		$arrFetchAllMessagedFriends = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFriendInfo,array()));
		
		$message=getSmileyDataForSave($_REQUEST["message"]);
		
		$mediaType="TEXT";
		if($_REQUEST["action"]=="uploadMediaMessage") //Saving Media Files
		{
			$fileControlID=$_REQUEST["messageType"]."_file";
			
			$ext = explode('.', basename( $_FILES[$fileControlID]['name']));
			if(in_array(strtolower($ext[count($ext)-1]),array("jpg","jpeg","png","bmp","gif","mp3","wav","doc","docx","pdf","ppt","pptx","txt")))
			{
				$display_path = 'uploads/message_media/'.$loginUserID;
				$target_path=$display_path;
				if(!file_exists($target_path))
		        {
					mkdir($target_path,0777);
				}
				$fileName=$_FILES[$fileControlID]['name'];
				$Fullpath=$target_path ."/".$fileName;
				if(move_uploaded_file($_FILES[$fileControlID]['tmp_name'], $Fullpath)) 
				{
					if(in_array(strtolower($ext[count($ext)-1]),array("jpg","jpeg","png","bmp","gif")))
					{
						$mediaType="IMAGE";
					}
					else if(in_array(strtolower($ext[count($ext)-1]),array("vob","mp4","3gp")))
					{
						$mediaType="VIDEO";
					}
					else if(in_array(strtolower($ext[count($ext)-1]),array("mp3","wav")))
					{
						$mediaType="AUDIO";
					}
					else if(in_array(strtolower($ext[count($ext)-1]),array("doc","docx","pdf","ppt","pptx","txt")))
					{
						$mediaType="DOCUM";
					}
					else
					{
						echo "ERROR|~|~|Please upload valid file..";
						return false;
					}
					$message= $display_path."/".$fileName;
					
				}
			}
			else
			{
				echo "ERROR|~|~|Please upload valid file...";
				return false;
			}	
		}
		
		
		if(($_REQUEST["action"]=="sendMessages" && trim($_REQUEST["message"])!="") || $_REQUEST["action"]=="uploadMediaMessage")//Save data in DB
		{
			$strInsert = "insert into cometchat set `from`='".$loginUserID."',`to`=:to,message=:message,sent='".time()."',`read`=0,message_type='".$mediaType."'";
			$pdodbobj->query($strInsert,array(":to"=>$recentFriend,":message"=>trim($message)));
		}
		
		//Fetching Messages
		$condition=" and id>".$_REQUEST["firstmessageid"];
		$qryFetchFullMessages="select * from cometchat where ((`from`='".$loginUserID."' and `to`='".$recentFriend."') or (`to`='".$loginUserID."' and `from`='".$recentFriend."'))  and message_status='".$message_status."' ".$condition." order by sent desc";
		$arrFetchFullMessages = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFullMessages,array()));
		
		//make all unread as read
		$qryMakeRead="update cometchat set `read`=1 where `to`='".$loginUserID."' and `from`='".$recentFriend."'  and message_status='".$message_status."' ".$condition;
		$pdodbobj->query($qryMakeRead,array());
		
		echo "SUCCESS|~|~|";
		$firstMessageID=$_REQUEST["firstmessageid"];
		
		if(count($arrFetchFullMessages)>0)
		{
			$firstMessageID=$arrFetchFullMessages[0]["id"];
			for($j=(count($arrFetchFullMessages)-1); $j>=0; $j--)
			{
				$sentMsgDate=getTimeAgo($arrFetchFullMessages[$j]["sent"], time());
		?>
				<tr  class="right_side_message_tr">
			<?php
				if($arrFetchFullMessages[$j]["from"]!=$loginUserID)
				{
			?>
					<td class="delete_box_td" >
						<input type="checkbox" id="delete_box_checkbox_<?php echo $arrFetchFullMessages[$j]["id"]; ?>" name="delete_box_checkbox[]" class="delete_box_checkbox" value="<?php echo $arrFetchFullMessages[$j]["id"]; ?>" />
					</td>
					<td width="62" style="padding:0px;">
						<div class="noti_pic">
							<a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrFetchAllMessagedFriends[0]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a>
						</div>
					</td>
					<td width="319" valign="top" style="text-align:left;">
						<div style="position:relative; top:0px"><span class="noti_name" style="margin-top:5px;"><a href="javascript:void(0)"><?php echo $arrFetchAllMessagedFriends[0]["Full_Name"] ?></a></span></div>
						<div class="separator"></div>
						<span style="margin-top:10px; float:left; margin-right:5px;">
						<?php 
							displayFormatedMessage($arrFetchFullMessages[$j]["message"],$arrFetchFullMessages[$j]["message_type"]);
						?>
						</span>
					</td>
					<td width="77" align="right" valign="top">
						<span style="margin-top:5px; float:right; margin-right:5px;"><?php echo $sentMsgDate; ?></span>
					</td>
			<?php		
				}
				else
				{
			?>
					<td width="77" align="right" valign="top">
						<span style="margin-top:5px; float:left; margin-right:5px;"><?php echo $sentMsgDate; ?></span>
					</td>
					<td width="319" valign="top" style="text-align:right;">
						<div style="position:relative; top:0px"><span class="noti_name" style="margin-top:5px;"><?php echo $arrLoginUserInfo[0]["Full_Name"] ?></span></div>
						<div class="separator"></div>
						<span style="margin-top:10px; float:right; margin-right:5px;">
						<?php 
							displayFormatedMessage($arrFetchFullMessages[$j]["message"],$arrFetchFullMessages[$j]["message_type"]);
						?>
						</span>
					</td>
					<td width="62" style="padding:0px;">
						<div class="noti_pic"><a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrLoginUserInfo[0]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a></div>
					</td>
					<td class="delete_box_td" >
						<input type="checkbox" id="delete_box_checkbox_<?php echo $arrFetchFullMessages[$j]["id"]; ?>" name="delete_box_checkbox[]" class="delete_box_checkbox" value="<?php echo $arrFetchFullMessages[$j]["id"]; ?>" />
					</td>
				<?php		
					}
				?>	
				</tr>
		<?php		
			}	
		}
		echo "|~|~|".$firstMessageID;	
	}
	else if($_REQUEST["action"]=="setReadUnread" && $_REQUEST["currentFriendInRightSide"]>0 )
	{
		$recentFriend=$_REQUEST["currentFriendInRightSide"];
		$condition="";
		echo "SUCCESS|~|~|";
		if($_REQUEST["readUnreadValue"]!=1)
		{
			$qryFetchLastMessages="select id from cometchat where `to`='".$loginUserID."' and `from`='".$recentFriend."'  and message_status='".$message_status."'  order by id desc limit 0, 1";
			$arrFetchLastMessages = $pdodbobj->fetch_array($pdodbobj->query($qryFetchLastMessages,array()));
		
			$condition = " and id='".$arrFetchLastMessages[0]["id"]."' ";
		}
		$qryMakeRead="update cometchat set `read`='".$_REQUEST["readUnreadValue"]."' where `to`='".$loginUserID."' and `from`='".$recentFriend."' and message_status='".$message_status."'  ".$condition;
		$pdodbobj->query($qryMakeRead,array());
		
		
		//Get Unread Message Count
		$qryFetchUnreadMessagesCount="select count(*) as unreadcount from cometchat where `to`='".$loginUserID."' and `read`=0  and message_status='".$message_status."' ";
		$arrFetchUnreadMessagesCount = $pdodbobj->fetch_array($pdodbobj->query($qryFetchUnreadMessagesCount,array()));
		echo $arrFetchUnreadMessagesCount[0]["unreadcount"];
		
	}
	else if($_REQUEST["action"]=="archiveOrSpamMessages" && $_REQUEST["currentFriendInRightSide"]>0 )
	{
		$recentFriend=$_REQUEST["currentFriendInRightSide"];
		$leftMesasgedFriend=0;
		$condition="";
		
		if(trim($_REQUEST["actionType"])=="3")
		{
			if(trim($_REQUEST["deleteMessageIds"])!="")
			{
				$qryArchiveDelete="update cometchat set message_status=:actionType where message_status='".$message_status."' and id in(".trim($_REQUEST["deleteMessageIds"]).") ".$condition;
				$pdodbobj->query($qryArchiveDelete,array(":actionType"=>$_REQUEST["actionType"]));
			}
			else
			{
				$qryArchiveDelete="update cometchat set message_status=:actionType where ((`to`='".$loginUserID."' and `from`='".$recentFriend."') or (`from`='".$loginUserID."' and `to`='".$recentFriend."'))  and message_status='".$message_status."'  ".$condition;
				$pdodbobj->query($qryArchiveDelete,array(":actionType"=>$_REQUEST["actionType"]));
			}
		}
		else if(trim($_REQUEST["actionType"])=="2" || trim($_REQUEST["actionType"])=="1" || trim($_REQUEST["actionType"])=="0")
		{
			$qryArchiveDelete="update cometchat set message_status=:actionType where ((`to`='".$loginUserID."' and `from`='".$recentFriend."') or (`from`='".$loginUserID."' and `to`='".$recentFriend."'))  and message_status='".$message_status."'  ".$condition;
			$pdodbobj->query($qryArchiveDelete,array(":actionType"=>$_REQUEST["actionType"]));
		}
		else
		{
			return "ERROR|~|~|Looks like there is some problem. Please try after some time.";
		}
		echo "SUCCESS|~|~|";
	    $qryFetchAllMessagedFriends="select af_users.*,tempchat.* from af_users, (select *,if(`from`='".$loginUserID."', `to`, `from`) as friendID from cometchat where (`from`='".$loginUserID."' or `to`='".$loginUserID."') and message_status='".$message_status."' order by sent desc) as tempchat where af_users.User_ID<>'".$loginUserID."'  and ((af_users.User_ID=tempchat.`from` and tempchat.`to`='".$loginUserID."' ) or (af_users.User_ID=tempchat.`to` and tempchat.`from`='".$loginUserID."')) group by af_users.User_ID limit ".$leftMesasgedFriend.", ".$totalRecordsPerPage;
	    $arrFetchAllMessagedFriends = $pdodbobj->fetch_array($pdodbobj->query($qryFetchAllMessagedFriends,array()));
	    
	    if(count($arrFetchAllMessagedFriends)>0)
	    {
			for($i=0; $i<count($arrFetchAllMessagedFriends) && $i<$totalRecordsPerPage; $i++)
			{
				//check All Read or not
				$qryCheckAllRead="select count(*) as unreadCount from cometchat where `to`='".$loginUserID."' and `from`='".$arrFetchAllMessagedFriends[$i]["friendID"]."' and `read`=0  and message_status='".$message_status."' ";
				$arrCheckAllRead = $pdodbobj->fetch_array($pdodbobj->query($qryCheckAllRead,array()));
						
				$leftMesasgedFriend++;
				$sentDate=getTimeAgo($arrFetchAllMessagedFriends[$i]["sent"], time());
		?>
			<tr <?php if($arrCheckAllRead[0]["unreadCount"]>0){ ?>style="background: #EEE;"<?php } ?> id="left_messaged_friend_tr_<?php echo $arrFetchAllMessagedFriends[$i]["User_ID"]; ?>" onclick="displayFullMessage('<?php echo $arrFetchAllMessagedFriends[$i]["User_ID"]; ?>');" >
			<?php
				if(($i+$leftMesasgedFriend)%2!=0)
				{
			?>
				<td class="delete_box_td" >
					<input type="checkbox" id="delete_box_checkbox_<?php echo $arrFetchFullMessages[$j]["id"]; ?>" name="delete_box_checkbox[]" class="delete_box_checkbox" value="<?php echo $arrFetchFullMessages[$j]["id"]; ?>" />
				</td>
				<td width="62" style="padding:0px;">
					<div class="noti_pic"><a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrFetchAllMessagedFriends[$i]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a></div>
				</td>
				<td width="319" valign="top" style="text-align:left;">
					<div style="position:relative; top:0px"><a href="javascript:void(0);"><span class="noti_name" style="margin-top:5px;"><?php echo $arrFetchAllMessagedFriends[$i]["Full_Name"] ?></span></a></div>
					<div class="separator"></div>
					<span style="margin-top:10px; float:left; margin-right:5px;">
					<?php 
						displayFormatedMessage($arrFetchAllMessagedFriends[$i]["message"],$arrFetchAllMessagedFriends[$i]["message_type"]);
					?>
					</span>
				</td>
				<td width="77" align="right" valign="top">
					<span style="margin-top:5px; float:right; margin-right:5px;"><?php echo $sentDate; ?></span>
				</td>
			<?php		
				}
				else
				{
			?>
				<td width="77" align="right" valign="top">
					<span style="margin-top:5px; float:left; margin-right:5px;"><?php echo $sentDate; ?></span>
				</td>
				<td width="319" valign="top" style="text-align:right;">
					<div style="position:relative; top:0px"><a href="javascript:void(0);"><span class="noti_name" style="margin-top:5px;"><?php echo $arrFetchAllMessagedFriends[$i]["Full_Name"] ?></span></a></div>
					<div class="separator"></div>
					<span style="margin-top:10px; float:right; margin-right:5px;">
					<?php 
						displayFormatedMessage($arrFetchAllMessagedFriends[$i]["message"],$arrFetchAllMessagedFriends[$i]["message_type"]);
					?>
					</span>
				</td>
				<td width="62" style="padding:0px;">
					<div class="noti_pic"><a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrFetchAllMessagedFriends[$i]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a></div>
				</td>
				<td class="delete_box_td" >
					<input type="checkbox" id="delete_box_checkbox_<?php echo $arrFetchFullMessages[$j]["id"]; ?>" name="delete_box_checkbox[]" class="delete_box_checkbox" value="<?php echo $arrFetchFullMessages[$j]["id"]; ?>" />
				</td>
			<?php		
				}
			?>	
			</tr>
		<?php		
			}
		}
		else
		{
		?>
			<tr>
				<td colspan="3">There is no messages.</td>
			</tr>
		<?php
		}
		echo "|~|~|".$leftMesasgedFriend; //Total messaged friends
		echo "|~|~|".$arrFetchAllMessagedFriends[0]["User_ID"]; //Upper most friend to display right side messages.
	}
	
	/*else if($_REQUEST["action"]=="getPreviousMessages" && $_REQUEST["friendId"]>0 && $_REQUEST["lastMessageID"]>0)
	{
		$qryLoginUserInfo="select * from af_users where User_ID='".$loginUserID."'";
		$arrLoginUserInfo = $pdodbobj->fetch_array($pdodbobj->query($qryLoginUserInfo,array()));
		
		$recentFriend=$_REQUEST["friendId"];
		$qryFetchFriendInfo="select * from af_users where User_ID='".$recentFriend."'";
		$arrFetchAllMessagedFriends = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFriendInfo,array()));
		
		$qryFetchFullMessages="select * from cometchat where ((`from`='".$loginUserID."' and `to`='".$recentFriend."') or (`to`='".$loginUserID."' and `from`='".$recentFriend."')) and id < '".$_REQUEST["lastMessageID"]."' order by sent desc limit ".$startFrom.", ".$totalRecordsPerPage;
		$arrFetchFullMessages = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFullMessages,array()));
		
		$qryFetchFullMessagesCount="select count(*) as totalRows from cometchat where ((`from`='".$loginUserID."' and `to`='".$recentFriend."') or (`to`='".$loginUserID."' and `from`='".$recentFriend."')) and id < '".$_REQUEST["lastMessageID"]."' order by sent limit ".$startFrom.", ".$totalRecordsPerPage;
		$arrFetchFullMessagesCount = $pdodbobj->fetch_array($pdodbobj->query($qryFetchFullMessagesCount,array()));
		
		if(count($arrFetchFullMessages)>0)
		{
			echo "SUCCESS|~|~|";
			$lastMessageID="";
			for($j=(count($arrFetchFullMessages)-1); $j>=0; $j--)
			{
				if($lastMessageID=="")
				{
					$lastMessageID=$arrFetchFullMessages[$j]["id"];
				}
				$sentMsgDate=getTimeAgo($arrFetchFullMessages[$j]["sent"], time());
		?>
				<tr  class="right_side_message_tr">
			<?php
				if($arrFetchFullMessages[$j]["from"]!=$loginUserID)
				{
			?>
					<td width="62" style="padding:0px;">
						<div class="noti_pic">
							<a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrFetchAllMessagedFriends[0]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a>
						</div>
					</td>
					<td width="319" valign="top" style="text-align:left;">
						<div style="position:relative; top:0px"><span class="noti_name" style="margin-top:5px;"><a href="javascript:void(0)"><?php echo $arrFetchAllMessagedFriends[0]["Full_Name"] ?></a></span></div>
						<div class="separator"></div>
						<span style="margin-top:10px; float:left; margin-right:5px;"><?php echo $arrFetchFullMessages[$j]["message"] ?></span>
					</td>
					<td width="77" align="right" valign="top">
						<span style="margin-top:5px; float:right; margin-right:5px;"><?php echo $sentMsgDate; ?></span>
					</td>
			<?php		
				}
				else
				{
			?>
					<td width="77" align="right" valign="top">
						<span style="margin-top:5px; float:left; margin-right:5px;"><?php echo $sentMsgDate; ?></span>
					</td>
					<td width="319" valign="top" style="text-align:right;">
						<div style="position:relative; top:0px"><span class="noti_name" style="margin-top:5px;"><?php echo $arrLoginUserInfo[0]["Full_Name"] ?></span></div>
						<div class="separator"></div>
						<span style="margin-top:10px; float:right; margin-right:5px;"><?php echo $arrFetchFullMessages[$j]["message"] ?></span>
					</td>
					<td width="62" style="padding:0px;">
						<div class="noti_pic"><a href="javascript:void(0);"><img src="<?php echo "uploads/image_crop.php?image=/".$arrLoginUserInfo[0]["Profile_Pic_Path"]."&width=48&height=45&cropratio=48:45";  ?>" /></a></div>
					</td>
				<?php		
					}
				?>	
				</tr>
		<?php		
			}
			echo "|~|~|".$lastMessageID;
			
			if($arrFetchFullMessagesCount[0]["totalRows"]>count($arrFetchFullMessages))
			{
				echo "|~|~|"."YES";
			}
			else
			{
				echo "|~|~|"."NO";
			}		
		}
		else
		{
			echo "ERROR|~|~|There is no more messages.";
		}
	}*/
}

function displayFormatedMessage($message, $messageType)
{
	if($messageType=="IMAGE")
	{
		echo '<a href="'.$message.'" class="PagePhotoGallery" data-fancybox-group="PagePhotoGallery"><img src="uploads/image_crop.php?image=/'.$message.'&width=30&height=30&cropratio=30:30" /></a>';
	}
	else if($messageType=="AUDIO" || $messageType=="VIDEO" || $messageType=="DOCUM")
	{
		if($messageType=="DOCUM") 
		{
			$messageType="DOCUMENT";
		}
		echo '<a href="'.$message.'" target="_blank">'.$messageType.'</a>';
	}
	else
	{
		echo $message;
	}
}
?>